AppendToObject<T, K, V>
使用例
code:ts
type Test = { id: '1' };
type Result = AppendToObject<Test, 'value', 4>; // expected to be { id: '1', value: 4 }
定義例
Mapped Typesを使って1つのRecordとして定義する
code:1.ts
type AppendToObject<
T extends Record<string, unknown>,
K extends string,
V
= {
};
条件分岐のある最も無難な定義という感じ
code:2.ts
type AppendToObject<
T extends Record<string, unknown>,
U extends string,
V,
= MergeIntersections< T & { K in U: V; } >; 最も直観的
code:3.ts
type AppendToObject<
T extends Record<string, unknown>,
K extends string,
V,
= {
};